package com.linwen.javase.juc.excise;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName ThreadCondition
 * @Description
 * @Author lin
 * @Date 2022/4/26 上午12:11
 * @Version V1.0
 *
 * 题目要求 ABC  ABC  循环依次打印20次
 * 使用线程通信
 */
public class ThreadCondition {
    public static void main(String[] args) {
        ThreadPrint threadPrint = new ThreadPrint();

        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                threadPrint.printA(i);
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                threadPrint.printB(i);
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                threadPrint.printC(i);
                System.out.println("+++++++++++++++++++++");
            }
        },"C").start();


    }
}

class ThreadPrint{
    // 标志位
    private int num = 1;

    // 锁
    private Lock lock = new ReentrantLock();
    // condition
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    /**
     * 打印A
     *
     * @Param loop 打印轮次
     * */
    public void printA(int loop){
        lock.lock();
        try{
            // 等待
            while (num!=1){
                try {
                    condition1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 打印A线程名
            System.out.println(Thread.currentThread().getName()+"  "+loop);

            // 唤醒B
            num = 2;
            condition2.signal();
        }finally {
            lock.unlock();
        }
    }

    /**
     * 打印B
     *
     * @Param loop 打印轮次
     * */
    public void printB(int loop){
        lock.lock();
        try{
            // 等待
            while (num!=2){
                try {
                    condition2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 打印B线程名
            System.out.println(Thread.currentThread().getName()+"  "+loop);

            // 唤醒C
            num = 3;
            condition3.signal();
        }finally {
            lock.unlock();
        }
    }

    /**
     * 打印C
     *
     * @Param loop 打印轮次
     * */
    public void printC(int loop){
        lock.lock();
        try{
            // 等待
            while (num!=3){
                try {
                    condition3.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 打印C线程名
            System.out.println(Thread.currentThread().getName()+"  "+loop);

            // 唤醒A
            num = 1;
            condition1.signal();
        }finally {
            lock.unlock();
        }
    }
}
